module IfItWerent4AR
  module Initialization
    module_function

    def get_schema
      YAML.load_file("#{Rails.root}/db/schema_IFWN4AR.yml") rescue raise("No 'schema_IFWN4AR.yml' found in 'db' folder").inspect
    end

    def init_mapping
      schema = get_schema
      schema.each_pair do |table_name, attrs_with_types|
        invalid_datatypes = schema[table_name].values.map do |datatype|
          raise("Invalid datatypes #{invalid_datatypes} detected for #{table_name} in schema").inspect if IfItWerent4AR::VALID_DATA_TYPES.exclude?(datatype)
        end

        IfItWerent4AR::Base.add_associations

        table_name.camelize.constantize.class_eval do
          attr_accessor *(attrs_with_types.symbolize_keys.keys)
          self.attributes_with_types = attrs_with_types
        end
      end
    end
  end
end
